package cn.com.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiAddress;
import cn.com.api.entity.ApiJuan;
import cn.com.api.entity.ApiT_juan_tj;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.U_cate;
import cn.com.api.mapper.ApiJuanMapper;
import cn.com.api.mapper.ApiJuanTJMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.U_cateMapper;
import cn.com.api.requestpage.CancelReasonPage;
import cn.com.api.requestpage.DianPingPage;
import cn.com.api.requestpage.OrderConfirmPage;
import cn.com.api.requestpage.OrderMessagePage;
import cn.com.api.requestpage.PaytypeConfirmPage;
import cn.com.api.requestpage.SearchOrdersPage;
import cn.com.api.requestpage.SendOrderPage;
import cn.com.api.requestpage.ShopOrderPage;
import cn.com.api.service.CheckService;
import cn.com.api.service.SystemService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class CheckServiceImpl implements CheckService {
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ApiJuanTJMapper apiJuanTJMapper;
	@Autowired
	private ApiJuanMapper apiJuanMapper;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	@Autowired
	private U_cateMapper u_cateMapper;
	@Autowired
	private ApiU_favoriteMapper apiU_favoriteMapper;

	@Override
	public boolean checkToken(String u_id, String token, Map<String, Object> map)
			throws Exception {
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户ID不可为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "令牌为空！");
			flag = false;
		}
		if (flag) {
			flag = systemService.isUserLogin(u_id, token);
			if (!flag) {
				map.put(Utils.RESULT, "0011");
				map.put(Utils.MESSAGE, "您的账号在另一台设备上登录，若非本人操作请注意账号安全！");
			}
		}

		return flag;
	}
	
	
	@Override
	public boolean checkRequestOrderConfirmPage(
			OrderConfirmPage orderConfirmPage, Map<String, Object> map)
			throws Exception {
		/** 用户名 */
		String u_id = orderConfirmPage.getU_id();
		/** token */
		String token = orderConfirmPage.getToken();
		/** 购物车ids 分隔，以","分隔 */
		String c_ids = orderConfirmPage.getC_ids();
		/** 收获地址 */
		Integer address_id = orderConfirmPage.getAddress_id();
		/** 备注 */
		String remark = orderConfirmPage.getRemark();
		/** 付款方式 */
		Integer payType = orderConfirmPage.getPayType();
		/** 配送费 */
		String sendfee = orderConfirmPage.getSendfee();
		/** 配送日期 */
		String sendDate = orderConfirmPage.getSendDate();
		/** 订单总金额 */
		String sumPrice = orderConfirmPage.getSumPrice();
		/** 优惠券 */
		String cashVoucher = orderConfirmPage.getCashVoucher();

		boolean flag = true;
		System.out.print("u_id-" + u_id + ",c_ids-" + c_ids + ",address_id-"
				+ address_id + ",remark-" + remark + ",payType-" + payType);
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(c_ids)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单内容为空！");
			flag = false;
		}
		if (flag && Util.isNull(address_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "未选择收货地址，请选择收货地址！");
			flag = false;
		}
		if (flag && Util.isNull(payType)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "未选择付款方式，请选择付款方式！");
			flag = false;
		}
		System.out.println("配送日期校验："+sendDate);
		if (flag && Util.isNull(sendDate)) {
			 //配送日期是空的话，从配置表取取值设定
//			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//			Date date = new Date();
//			String sendStr = dateFormat.format(date);
//			int hour = (date.getHours() + 2);
//			sendDate = sendStr +" "+ hour + ":00-" + hour + ":30";
//			orderConfirmPage.setSendDate(sendDate);
		} else if(flag && !Util.isNull(sendDate)){
			//不为空 && !Util.validateSendTime(sendDate)
			//解析日期：1,2016-01-15 12:30-13:00;41,2016-01-15 12:30-13:00    店铺id1,配送日期1;店铺id2,配送日期2
			String[] shopDateArray = sendDate.split(";");
			Map<Integer, String> sendTimeMap = new HashMap<Integer, String>();
			if(shopDateArray!=null && shopDateArray.length>0){
				for(String shopDateStr: shopDateArray){
					int leng = shopDateStr.indexOf(",");
					Integer shop_id = Integer.parseInt(shopDateStr.substring(0, leng));
					String shopDate = shopDateStr.substring(leng + 1);
					System.out.println(shop_id + ":" + shopDate);
					if(!Util.isNull(shopDate)){//配送日期不为空--为空，取配置表的
						if(Util.validateSendTime(shopDate)){//校验成功，格式正确
							
						}else{
							//校验配送日期格式是否正确
							map.put(Utils.RESULT, "0002");
							map.put(Utils.MESSAGE, "配送日期格式不正确，yyyy-MM-dd HH:mm-HH:mm");
							flag = false;
							break;
						}
					}
					sendTimeMap.put(shop_id, shopDate);
				}
			}
			orderConfirmPage.setSendTimeMap(sendTimeMap);
		}
		if (flag && !Util.isNull(cashVoucher)) {// 使用了优惠券
			String[] cashes = cashVoucher.split(";");
			if (cashes != null && cashes.length > 0) {
				boolean cashFlag = false;// 使用优惠券的标志
				int length = 0;// 使用优惠券的个数
				Map<String, Integer> cashIdMap = new HashMap<String, Integer>();
				Map<Integer, Integer> cashMap = new HashMap<Integer, Integer>();
				for (String cash : cashes) {// :分隔
					int leng = cash.indexOf(":");
					Integer shop_id = Integer.parseInt(cash.substring(0, leng));
					String cashid = cash.substring(leng + 1);
					System.out.println(shop_id + ":" + cashid);
					if (!Util.isNull(cashid)) {// 优惠券不为空
						cashFlag = true;// 使用优惠券
						length = length + 1;
						if(cashIdMap.containsKey(cashid)){
							Integer count = cashIdMap.get(cashid);
							count = count + 1;
							cashIdMap.put(cashid, count);
						}else{
							cashIdMap.put(cashid, 1);
						}
						
						Integer cashId = Integer.parseInt(cashid);
						cashMap.put(shop_id, cashId);// 商铺使用的优惠券
					}
				}
				if (cashFlag) {
					if (length == cashIdMap.size()) {// 使用优惠券的个数 和
														// 不同的优惠券个数一样---说明没有重复的优惠券
						orderConfirmPage.setCashMap(cashMap);
						System.out.println("正常使用优惠券！");
					} else {//有重复使用的优惠券
						//被重复使用的优惠券个数是否够
						orderConfirmPage.setCashMap(cashMap);
						for (Map.Entry<String, Integer> entry : cashIdMap.entrySet()) {
							System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
							String cashid = entry.getKey();
							Integer count = entry.getValue();
							if(count>1){//优惠券数量大于1的，判断优惠券个数是否够用
								//根据u_id cashid 查询优惠券
								ApiT_juan_tj juan_tj = apiJuanTJMapper.selectApiT_juan_tjByUidAndJid(u_id, Integer.valueOf(cashid));
								if(juan_tj!=null){
									Integer juan_count = juan_tj.getCount();
									if(juan_count>=count){//用户拥有的优惠券数量大于使用的数量
										
									}else{
										flag = false;
										ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.valueOf(cashid));
										map.put(Utils.RESULT, "0002");
										map.put(Utils.MESSAGE, "您拥有“"+juan.getC_titles()+"”优惠券"+juan_tj.getCount()+"张,最多可使用"+juan_tj.getCount()+"次");
										System.out.println("重复使用优惠券！");
										break;
									}
								}
							}
						}
						
					}
				}
			}

		}
		return flag;
	}

	@Override
	public boolean checkPaytypeConfirmPage(
			PaytypeConfirmPage paytypeConfirmPage, Map<String, Object> map)
			throws Exception {
		/** 用户名 */
		String u_id = paytypeConfirmPage.getU_id();
		/** token */
		String token = paytypeConfirmPage.getToken();
		/** 使用余额金额 */
		Double useCmoney = paytypeConfirmPage.getUseCmoney();
		/** 总需支付金额 */
		Double sumPrice = paytypeConfirmPage.getSumPrice();
		/** 订单号 */
		String orderIds = paytypeConfirmPage.getOrderIds();
		/** 在线支付方式 ----余额：0 1-支付宝 2-微信 4-银联 */
		String payType = paytypeConfirmPage.getPayType();
		/** 支付订单号 */
		String orderIdFlag = paytypeConfirmPage.getOrderIdFlag();
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && useCmoney == null) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "使用余额不能为空！");
			flag = false;
		}
		if (flag && sumPrice == null) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "总需支付金额不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderIds)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		}else{
			//订单号不为空的话，校验erp库存是否充足
//			String[] orderIdArray = orderIds.split(",");
//			List<ApiT_products> list = apiT_productsMapper.findProductNotEnoughByOrdersId(orderIdArray);
//			if(list!=null && list.size()>0){
//				map.put(Utils.RESULT, "0002");
//				map.put(Utils.MESSAGE, list.get(0).getName()+"库存不足，请取消订单！若未取消订单会在20分钟后自动取消！");
//				flag = false;
//			}//else erp库存充足
			
		}
		if (flag && Util.isNull(payType)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "支付方式不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderIdFlag)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "支付订单号不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkParams(Map<String, String> parammap,
			Map<String, Object> map) {
		boolean flag = true;
		if (parammap.size() > 0) {
			for (String key : parammap.keySet()) {
				if (flag && Util.isNull(parammap.get(key))) {
					map.put(Utils.RESULT, "0002");
					map.put(Utils.MESSAGE, key + " 不可为空！");
					flag = false;
					break;
				}
			}
		}
		return flag;
	}

	@Override
	public boolean checkChargeMoney(Map<String, String> parammap,
			Map<String, Object> map) {
		String u_id = parammap.get("u_id");
		String token = parammap.get("token");
		String money = parammap.get("money");
		String payType = parammap.get("payType");
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(money)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "充值金额不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(payType)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "充值方式不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkAddCar(Map<String, String> parammap,
			Map<String, Object> map) {
		String u_id = parammap.get("u_id");
		String goods_id = parammap.get("goods_id");
		String product_id = parammap.get("product_id");
		String addFlag = parammap.get("addFlag");
		String token = parammap.get("token");
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(goods_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "选择商品不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(addFlag)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "增减商品标识不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(product_id)) {// 目前product_id为空，先默认设置为1
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "货品id不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {// 校验token
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkAddCar2(Map<String, String> params,
			Map<String, Object> map) {
		String u_id = params.get("u_id");
		String goods_id = params.get("goods_id");
		String product_id = params.get("product_id");
		String count = params.get("count");
		String token = params.get("token");
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(goods_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "选择商品不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(count)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "增减商品标识不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(product_id)) {// 目前product_id为空，先默认设置为1
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "货品id不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {// 校验token
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkMyOrder(SearchOrdersPage searchOrdersPage,
			Map<String, Object> map) throws Exception {
		String u_id = searchOrdersPage.getU_id();
		Integer orderStatus = searchOrdersPage.getOrderStatus();// 正处理，已完成，已取消
		Integer pageCount = searchOrdersPage.getPageCount();
		String searchDate = searchOrdersPage.getSearchDate();
		String token = searchOrdersPage.getToken();
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderStatus)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单请选择订单状态！");
			flag = false;
		}
		if (flag && Util.isNull(pageCount)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "页数不能为空！");
			flag = false;
		}
		if (flag && !Util.isNull(searchDate)) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(searchDate.trim());
			searchOrdersPage.setDate(date);
		}
		return flag;
	}

	@Override
	public boolean checkLogin(Map<String, String> parammap,
			Map<String, Object> map) {
		String c_mob = parammap.get("cMob");// 手机号
		String cPass = parammap.get("cPass");// 密码
		boolean flag = true;
		if (flag && Util.isNull(c_mob)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "手机号不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(cPass)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "密码不能为空！");
			flag = false;
		}
		if (flag && apiTmemberMapper.findMemberByCuserid(c_mob) == null) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "手机号不存在！");
			flag = false;
		}
		if (flag
				&& apiTmemberMapper.login(c_mob,
						CipherUtil.generatePassword(cPass)) == null) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "密码错误！");
			flag = false;
		}

		return flag;
	}

	@Override
	public boolean checkDianPing(DianPingPage dianPingPage,
			Map<String, Object> map) throws Exception {
		boolean flag = true;
		if (flag && Util.isNull(dianPingPage.getU_id())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名为空！");
			flag = false;
		}
		if (flag && Util.isNull(dianPingPage.getToken())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token为空！");
			flag = false;
		}
		if (flag && Util.isNull(dianPingPage.getC_dtype())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "请点评商品质量！");
			flag = false;
		}
		if (flag && Util.isNull(dianPingPage.getC_text())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "点评内容为空，请点评商品！");
			flag = false;
		} else {
			String c_text = dianPingPage.getC_text();
			dianPingPage.setC_text(c_text);
		}
		return flag;
	}

	@Override
	public boolean checkCancelReasonPage(CancelReasonPage cancelReasonPage,
			Map<String, Object> map) throws Exception {
		boolean flag = true;
		if (flag && Util.isNull(cancelReasonPage.getU_id())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(cancelReasonPage.getToken())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(cancelReasonPage.getTitles())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		}
		String name = cancelReasonPage.getName();
		if (flag && Util.isNull(name)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "姓名不能为空！");
			flag = false;
		} else {
			cancelReasonPage.setName(name);
		}
		if (flag && Util.isNull(cancelReasonPage.getMobile())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "联系电话不能为空！");
			flag = false;
		}
		String cancelReason = cancelReasonPage.getCancelReason();
		if (flag && Util.isNull(cancelReason)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "取消原因不能为空！");
			flag = false;
		} else {
			cancelReasonPage.setCancelReason(cancelReason);
		}
		return flag;
	}

	@Override
	public boolean checkShopOrders(SearchOrdersPage searchOrdersPage,
			Map<String, Object> map) throws Exception {
		String shop_id = searchOrdersPage.getShop_id();
		Integer orderStatus = searchOrdersPage.getOrderStatus();// 正处理，已完成，已取消
		Integer pageCount = searchOrdersPage.getPageCount();
		String searchDate = searchOrdersPage.getSearchDate();
		String token = searchOrdersPage.getToken();
		boolean flag = true;
		if (flag && Util.isNull(shop_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (Util.isNull(orderStatus)) {// 考虑如果为空的话，默认全部订单
			searchOrdersPage.setOrderStatus(9);// 全部订单
		}
		if (flag && Util.isNull(pageCount)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "页数不能为空！");
			flag = false;
		}
		if (flag && !Util.isNull(searchDate)) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(searchDate.trim());
			searchOrdersPage.setDate(date);
		}
		return flag;
	}

	@Override
	public boolean checkOrderMessagePage(OrderMessagePage orderMessagePage,
			Map<String, Object> map) throws Exception {
		boolean flag = true;
		if (flag && Util.isNull(orderMessagePage.getU_id())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderMessagePage.getToken())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderMessagePage.getShop_id())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "商铺id不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(orderMessagePage.getOrderId())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkShopOrderPage(ShopOrderPage shopOrderPage,
			Map<String, Object> map) {
		boolean flag = true;
		if (flag && Util.isNull(shopOrderPage.getOrderId())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(shopOrderPage.getToken())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkSendOrderPage(SendOrderPage sendOrderPage,
			Map<String, Object> map) {
		boolean flag = true;
		if (flag && Util.isNull(sendOrderPage.getOrderId())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(sendOrderPage.getToken())) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkSendOrderPageIdentifyCode(SendOrderPage sendOrderPage,
			Map<String, Object> map) {
		String identifyCode = sendOrderPage.getIdentifyCode();
		String orderId = sendOrderPage.getOrderId();
		String token = sendOrderPage.getToken();
		String code = "";
		boolean flag = true;
		if (flag && Util.isNull(orderId)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "订单号不能为空！");
			flag = false;
		} else {
			int length = orderId.length();
			code = orderId.substring(length - 4, length);
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(identifyCode)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "验证码不能为空！");
			flag = false;
		}
		// 校验验证码是否正确
		if (flag && !identifyCode.equals(code)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "验证码不正确！");
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkChargeGiftPage(OrderConfirmPage orderConfirmPage,
			Map<String, Object> map) {
		String u_id = orderConfirmPage.getU_id();
		String product_ids = orderConfirmPage.getProduct_ids();
		String chargeMoney = orderConfirmPage.getSumPrice();
		String token = orderConfirmPage.getToken();
		String sendDate = orderConfirmPage.getSendDate();
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(product_ids)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "商品不能为空！");
			flag = false;
		}
		// 校验验证码是否正确
		if (flag && Util.isNull(chargeMoney)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "充值金额不能为空！");
			flag = false;
		}
		System.out.println("配送日期校验："+sendDate);
		if (flag && Util.isNull(sendDate)) {

		} else if(flag && !Util.isNull(sendDate)){
			//不为空 && !Util.validateSendTime(sendDate)
			//解析日期：1,2016-01-15 12:30-13:00;41,2016-01-15 12:30-13:00    店铺id1,配送日期1;店铺id2,配送日期2
			String[] shopDateArray = sendDate.split(";");
			Map<Integer, String> sendTimeMap = new HashMap<Integer, String>();
			if(shopDateArray!=null && shopDateArray.length>0){
				for(String shopDateStr: shopDateArray){
					int leng = shopDateStr.indexOf(",");
					Integer shop_id = Integer.parseInt(shopDateStr.substring(0, leng));
					String shopDate = shopDateStr.substring(leng + 1);
					System.out.println(shop_id + ":" + shopDate);
					if(!Util.isNull(shopDate)){//配送日期不为空--为空，取配置表的
						if(Util.validateSendTime(shopDate)){//校验成功，格式正确
							
						}else{
							//校验配送日期格式是否正确
							map.put(Utils.RESULT, "0002");
							map.put(Utils.MESSAGE, "配送日期格式不正确，yyyy-MM-dd HH:mm-HH:mm");
							flag = false;
							break;
						}
					}
					sendTimeMap.put(shop_id, shopDate);
				}
			}
			orderConfirmPage.setSendTimeMap(sendTimeMap);
		}
		
		return flag;
	}
	
	@Override
	public boolean checkPointsOrderPage(OrderConfirmPage orderConfirmPage,
			Map<String, Object> map) {
		String u_id = orderConfirmPage.getU_id();
		String act_ids = orderConfirmPage.getAct_ids();
		String token = orderConfirmPage.getToken();
		String sendDate = orderConfirmPage.getSendDate();
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(act_ids)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "商品不能为空！");
			flag = false;
		}
		System.out.println("配送日期校验："+sendDate);
		if (flag && Util.isNull(sendDate)) {

		} else if(flag && !Util.isNull(sendDate)){
			//不为空 && !Util.validateSendTime(sendDate)
			//解析日期：1,2016-01-15 12:30-13:00;41,2016-01-15 12:30-13:00    店铺id1,配送日期1;店铺id2,配送日期2
			String[] shopDateArray = sendDate.split(";");
			Map<Integer, String> sendTimeMap = new HashMap<Integer, String>();
			if(shopDateArray!=null && shopDateArray.length>0){
				for(String shopDateStr: shopDateArray){
					int leng = shopDateStr.indexOf(",");
					Integer shop_id = Integer.parseInt(shopDateStr.substring(0, leng));
					String shopDate = shopDateStr.substring(leng + 1);
					System.out.println(shop_id + ":" + shopDate);
					if(!Util.isNull(shopDate)){//配送日期不为空--为空，取配置表的
						if(Util.validateSendTime(shopDate)){//校验成功，格式正确
							
						}else{
							//校验配送日期格式是否正确
							map.put(Utils.RESULT, "0002");
							map.put(Utils.MESSAGE, "配送日期格式不正确，yyyy-MM-dd HH:mm-HH:mm");
							flag = false;
							break;
						}
					}
					sendTimeMap.put(shop_id, shopDate);
				}
			}
			orderConfirmPage.setSendTimeMap(sendTimeMap);
		}
		
		return flag;
	}
	
	@Override
	public boolean checkOrderAddressPage(OrderConfirmPage orderConfirmPage,
			Map<String, Object> map) {
		String u_id = orderConfirmPage.getU_id();
		String c_ids = orderConfirmPage.getC_ids();
		String product_ids = orderConfirmPage.getProduct_ids();
		Integer address_id = orderConfirmPage.getAddress_id();
		String token = orderConfirmPage.getToken();
		boolean flag = true;
		if (flag && Util.isNull(u_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "用户名不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(token)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "token不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(c_ids) && Util.isNull(product_ids)) {//有一个不为空即可
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "商品不能为空！");
			flag = false;
		}
		if (flag && Util.isNull(address_id)) {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "未选择收货地址，请选择收货地址！");
			flag = false;
		}
		return flag;
	}
	
	@Override
	public boolean checkAddress(ApiAddress apiAddress, Map<String, Object> map) {
		//校验收货地址的经纬度
		boolean flag = true;
		String longitude = apiAddress.getLongitude();
		String latitude = apiAddress.getLatitude();
		if(Util.isNull(longitude) || Util.isNull(latitude)){
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "收货地址的经纬度不允许为空！");
			flag = false;
		}
		return flag;
	}
	
	@Override
	public void setHeadLineDefaultCate(String user_id)
	{
		PageInfo page = new PageInfo();
		page.setCurrentPage(1);
		page.setShowCount(10);
		List<U_cate> cate_list = this.u_cateMapper.getArticleCatesListPage(page);
		for(U_cate cate : cate_list)
		{
			ApiU_favorite favor = new ApiU_favorite();
			favor.setConn_id(cate.getId());
			favor.setType(3);
			favor.setUser_id(Integer.valueOf(user_id));
			favor.setAdd_time(Util.CurrentTime());
			this.apiU_favoriteMapper.insertU_favorite(favor);
			
		}
	}

}
